#include <file.au3>
#include <Array.au3>
#include <Variable.au3>
#include <Func.au3>

;===== [EX_S]
;$E_file
;$G_Action
;Action= 1 (Install) or 2 (Uninstall) or 3 (Upgrade)
;Const $ConfigPara				= "Config Parameter"
;Const $ConfigPara_key			= "Config_File_Name"
;===== [EX_E]

;[temp_test_S]
;#cs
$exception_item_location 	= @ScriptDir & "\config\ExceptionItem"
$exception_file  			= $exception_item_location & "\ExceptionItem-"& $G_DS_version &".ini"	;[Important]

MsgBox(64,"","1. expected_file : " & @CRLF & $E_file & @CRLF & @CRLF & "2. exception_file : " & @CRLF & $exception_file)

generate_expect_result($exception_file,$E_file)

MsgBox(64,"","Generate expected file complete!")
;#ce
;[temp_test_E]


Func generate_expect_result($exception_file,$output_file)
     Local $i,$j,$action
	 Dim $record
	 

	 FileCopy ($config_filename, $output_file)
	 $config_ini_section_index = IniReadSectionNames($output_file)	 
	 ;MsgBox(0,"config",$config_ini_section_index)
	 
	 $action=IniRead($globalvariable_file_name,$BuildInfo,"Action","3")
	 ;MsgBox(0,"Action",$action)

	 ;_ArrayDisplay($config_ini_section_index, "$Array")
	 
	 
	 For $i=1 to $config_ini_section_index[0]

		$item=IniReadSection ($config_filename, $config_ini_section_index[$i] )
		;MsgBox(0,"$item",$item[0][0])
		
		;_ArrayDisplay($item, "Item")

		Switch $config_ini_section_index[$i]
			Case "Service"
					if $action="1" Or $action="3" Then
						for $j=1 to $item[0][0]
						IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";1")
						Next
					Else
						for $j=1 to $item[0][0]
						IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";NA")
						Next
					EndIf

			Case "Folder"
					if $action="1" Or $action="3" Then
						for $j=1 to $item[0][0]
						IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";1")
						Next
					Else
						for $j=1 to $item[0][0]
						IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";NA")
						Next
					EndIf

			Case "File"
					if $action="1" Or $action="3" Then
						local $disk_version=IniRead($globalvariable_file_name,"Global Variables","DiskSafe_Version","3.62")
						local $disk_build=IniRead($globalvariable_file_name,"Global Variables","DiskSafe_Build","2.40")
						local $ima_version=IniRead($globalvariable_file_name,"Global Variables","IMA_Version","2.5")
						local $ima_build=IniRead($globalvariable_file_name,"Global Variables","IMA_Build","398")

						for $j=1 to $item[0][0]
							
							if Not 0=StringInStr($item[$j][1],"$Disksafe") Then
								IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";"&$disk_version&".0."&$disk_build)
							EndIf
							
							if Not 0=StringInStr($item[$j][1],"$SYSTEMDIR\drivers") Then
								IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";"&$disk_version&".0."&$disk_build)
							EndIf
						
							if Not 0=StringInStr($item[$j][1],"$IMA") Then
								IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";"&$ima_version&".0."&$ima_build)
							EndIf
							
						Next
						
					Else
						;MsgBox(0,"","IS Uninstall")
						for $j=1 to $item[0][0]
							IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";NA")
						Next
					EndIf
					
			Case "Registry"
					
					;1 (Install) or 2 (Uninstall) or 3 (Upgrade)
					
					if $action="1" Or $action="3" Then
						for $j=1 to $item[0][0]
							
							IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";---")
						Next
						
					ElseIf $action="2" Then
						for $j=1 to $item[0][0]
						
							IniWrite($output_file,$config_ini_section_index[$i],$item[$j][0],$item[$j][1]&";NA")
						Next
					
					EndIf	
						
		EndSwitch
				
	Next
	
	;MsgBox(0,"","Before Replace")
	
	_ReplaceExceptionInFile($exception_file, $output_file)

			
EndFunc

#cs
;test_S
$exception_item_location 	= @ScriptDir & "\config\ExceptionItem"
$exception_file  			= $exception_item_location & "\ExceptionItem-"& $G_DS_version &".ini"	;[Important]

_ReplaceExceptionInFile($exception_file, $E_file)
MsgBox(0,"","Complete....")
;test_E
#ce

Func _ReplaceExceptionInFile($exceptionFile, $expectedFile)
	
	IniReadSectionNames($exceptionFile)
	
	$excep_sectionName = IniReadSectionNames($exceptionFile)
	
	
	If @error Then 
		MsgBox(4096, "", "Error occurred, probably no INI file.")
	Else
		For $i = 1 To $excep_sectionName[0]
			
			$excep_section = IniReadSection($exceptionFile,$excep_sectionName[$i])			
		
		;>>	 	
			If @error Then 
				MsgBox(4096, "", "Error occurred, probably no INI file.")
			Else				
				Switch $excep_sectionName[$i]
				
					Case "File"

					For $j = 1 To $excep_section[0][0]
						
						$expected_section = IniReadSection($expectedFile,"File")	
						For $k = 1 to $expected_section[0][0]
							
							If StringInStr($expected_section[$k][1],";" & $excep_section[$j][0] & ";",1)<>0 Then
								$expected_Key = getItemValue_Key("File",$expected_section[$k][1])
								IniWrite($expectedFile,"File",$expected_section[$k][0],$expected_Key& $excep_section[$j][1])
							EndIf	
						Next	
						
					Next
				
					Case "Registry"
				
					;==========================================================
					For $j = 1 To $excep_section[0][0]
						
						$expected_section = IniReadSection($expectedFile,"Registry")	
						
						For $k = 1 to $expected_section[0][0]
							
							$excep_section_Split = StringSplit($excep_section[$j][0], "-")
							
							If $excep_section_Split[1] = "disk" Then
								$regType = "$Disk_Policy_GUID*"
							ElseIf $excep_section_Split[1] = "group" Then
								$regType = "$Group_Policy_GUID*"
							EndIf
								
							$compared1 = StringInStr($expected_section[$k][1],$regType & ";",1)
							$compared2 = StringInStr($expected_section[$k][1],";" & $excep_section_Split[2] & ";",1)
							
							;====
							If $compared1<>0 And $compared2<>0 Then
								$expected_Key = getItemValue_Key("Registry",$expected_section[$k][1])
								
								IniWrite($expectedFile,"Registry",$expected_section[$k][0],$expected_Key& $excep_section[$j][1])
							
							EndIf
							;====
							
						Next	
						
					Next
				
					;==========================================================
				
				EndSwitch	

			EndIf			
		;<<	
			;MsgBox(4096, "", $excep_sectionName[$i])
		
		Next
	EndIf

EndFunc






#cs
For $x = 1 to $aRecords[0]
	Msgbox(0,'Record:' & $x, $aRecords[$x])
Next
#ce


#cs
$aArray1 = _StringBetween('$PROGRAMFILESDIR\', '$', '\');Not using SRE
_ArrayDisplay($aArray1, 'Default Search')
$aArray2 = _StringBetween('[18][20][3][5][500][60]', '\[', '\]', -1, 1);Using SRE
_ArrayDisplay($aArray2, 'StringRegExp Search')

$text = StringReplace("this is a line of text", " ", "-")
$numreplacements = @extended
MsgBox(0, "New string is", $text)
MsgBox(0, "The number of replacements done was", $numreplacements)


MsgBox(0, "Regular Expression Replace Test", StringRegExpReplace("$Where have all the flowers gone, long time passing?", "$", ""))

$var = IniReadSection("GlobalVariables.ini", "Global Variables")
;_ArrayDisplay($var, "$avArray")

$iIndex = _ArraySearch($var, "PROGRAMFILESDIR", 0, 0, 0, 1)
If @error Then
    MsgBox(0, "Not Found", "PROGRAMFILESDIR", 'was not found in the array.')
Else
    MsgBox(0, "Found", 'PROGRAMFILESDIR was found in the array at position ' & $iIndex & ".")
	MsgBox(0, "Found", "PROGRAMFILESDIR's value was found in the array at position " & $var[$iIndex][1] & ".")
	$result = StringRegExp($var[$iIndex][1], "@")
    If $result = 1 Then
	MsgBox(0, "Search result:", "PASS")
	EndIf
EndIf



If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0][0]
        ;MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
		Local $temp=StringSplit($var[$i][1],"\")
		;_ArrayDisplay($temp, "Array")
    Next
EndIf

$days = StringSplit("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",")
;$days[1] contains "Sun" ... $days[7] contains "Sat"



$var = IniReadSectionNames("GlobalVariables.ini")
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0]
        MsgBox(4096, "", $var[$i])
    Next
EndIf
#ce
